function PNGOverview2(varargin)
rootDir = varargin{1};
mode = varargin{2};
flagGate = varargin{3};

if strcmp(mode,'FTFit')
    inImgDir = [rootDir filesep 'FTFitData' filesep];
    sepChar = filesep;
elseif strcmp(mode,'EEta')
    inImgDir = [rootDir filesep 'ChannelDefoExport' filesep];
    sepChar = '_';
else
    error('no mode selected!');
end

if flagGate
    strGate = '_gate';
else
    strGate = '_noGate';
end

outImgDir = rootDir;

%% find measurement name
idx = find(rootDir == filesep);
measName = rootDir(idx+1:end);

%% find samples and choose orientation
dirRes = dir(inImgDir);
dirRes = dirRes([dirRes.isdir]);
sampleList = cell(1,length(dirRes)-2);
flowRateList = cell(1,length(dirRes)-2);
fprintf('Index\tName\t\t\tFlowRate\n');
mask = true(1,length(dirRes)-2);
for k = 3:length(dirRes)
    idxUnder = find(dirRes(k).name == '_');
    if length(idxUnder) >= 2
        sampleList{k-2} = dirRes(k).name(1:idxUnder(end-1)-1);
        flowRateList{k-2} = dirRes(k).name(idxUnder(end-1)+1:idxUnder(end)-1);
        fprintf('%d\t%s\t\t%s\n',k-2,sampleList{k-2},flowRateList{k-2});
    else
        mask(k-2) = false;
    end
end
sampleList = sampleList(mask); flowRateList = flowRateList(mask);
% idxMat = input('Choose sample orientation: ');
if length(sampleList) == 3
    idxMat = 1:3;
else
    idxMat = [1:3;4:6];
end
sampleList = sampleList(idxMat);
flowRateList = flowRateList(idxMat);

%% constants
spaceX = 20;
spaceY = 20;

if strcmp(mode,'FTFit')
    listImages = {'Area','Velocity','Tau1','Tau2','DeltaDefoTau1','DeltaDefoTau2'};
elseif strcmp(mode,'EEta')
    listImages = {'eMod','tau2','visc','areaHisto','eModHisto','tau2Histo','viscHisto'};
end

imageExtension = 'png';
%% read image sizes
sizeImages = zeros(length(listImages),size(flowRateList,1),size(flowRateList,2),2);
for idxImages = 1:length(listImages)
    for idxX = 1:size(flowRateList,2)
        for idxY = 1:size(flowRateList,1)
%             fprintf('sample = %s, flow rate = %s\n', samples{idxY,idxX}, flowRates{idxY,idxX});
            filedir = dir([inImgDir sampleList{idxY,idxX} '_' flowRateList{idxY,idxX} '_*']);
            filedir = filedir([filedir.isdir]);
            img = imread([inImgDir filedir.name sepChar listImages{idxImages} strGate '.' imageExtension]);
            img = autoCrop(img);
            sizeImages(idxImages,idxY,idxX,1) = size(img,1);
            sizeImages(idxImages,idxY,idxX,2) = size(img,2);
        end
    end
end
%% concatenate images
for idxImages = 1:length(listImages)
    sizeImage = [max(max(sizeImages(idxImages,:,:,1))), max(max(sizeImages(idxImages,:,:,2)))];
    imgCat = repmat(uint8(255),size(flowRateList,1)*sizeImage(1)+(size(flowRateList,1)-1)*spaceY,size(flowRateList,2)*sizeImage(2)+(size(flowRateList,2)-1)*spaceX,3);
    
    for idxX = 1:size(flowRateList,2)
        for idxY = 1:size(flowRateList,1)
%             fprintf('sample = %s, flow rate = %s\n', samples{idxY,idxX}, flowRates{idxY,idxX});
            filedir = dir([inImgDir sampleList{idxY,idxX} '_' flowRateList{idxY,idxX} '_*']);
            filedir = filedir([filedir.isdir]);
            img = imread([inImgDir filedir.name sepChar listImages{idxImages} strGate '.' imageExtension]);
            img = autoCrop(img);
            img = imgExtend(img, sizeImage, 'c');
            imgCat(1+(idxY-1)*(sizeImage(1)+spaceY):idxY*sizeImage(1)+(idxY-1)*spaceY,...
                1+(idxX-1)*(sizeImage(2)+spaceX):idxX*sizeImage(2)+(idxX-1)*spaceX, :) = img;
        end
    end
    imwrite(imgCat, [outImgDir filesep measName '_result' strGate '_' listImages{idxImages} '.' imageExtension]);
end

end
%% funcrions
function img = funcAutoCut(img, flag)
if flag
    img = img(70:1130,150:1480,:,:);
else
    img = img(30:870,80:1120,:,:);
end
end

function img = autoCrop(img)
mask = (img == 255);
% mask = all(mask,4);
mask = all(mask,3);
maskX = all(mask,1);
maskY = all(mask,2);
idxMaskXZero = find(maskX == false);
idxMaskYZero = find(maskY == false);
img = img(idxMaskYZero(1):idxMaskYZero(end),idxMaskXZero(1):idxMaskXZero(end),:,:);

end

function imgExt = imgExtend(img, targetSize, mode)
if mode == 'c'
    shiftY = round((targetSize(1) - size(img,1))/2);
    shiftX = round((targetSize(2) - size(img,2))/2);
    imgExt = repmat(uint8(255),targetSize(1),targetSize(2),3);
    imgExt(1+shiftY:1+shiftY+size(img,1)-1, 1+shiftX:1+shiftX+size(img,2)-1, :) = img;
end
end